//
//  XQOrderPublicView.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/2/6.
//

import UIKit

class XQOrderPublicView: UIView {
    
    var onTapHandle: (() -> Void)?

    private lazy var imageView : UIImageView = {
        let imageView = UIImageView()
        return imageView
    }()
    
    private lazy var photoView : UIView = {
        let view = UIView()
        return view
    }()
    
    private lazy var photoImageView : UIImageView = {
        let imageView = UIImageView()
        return imageView
    }()
    
    private lazy var contentLb : UILabel = {
        let lb = UILabel()
        lb.textColor = .xq_black
        lb.textAlignment = .right
        lb.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        return lb
    }()
    
    private lazy var btn : UIButton = {
        let btn = UIButton(type: .custom)
        btn.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
        return btn
    }()
    
    var placeHolderName : String = ""
    
    var imageName : String? = "" {
        didSet {
            if let url = URL(string: imageName ?? "") {
                self.imageView.kf.setImage(with: url,placeholder: UIImage(named: placeHolderName))
            }else {
                self.imageView.image = UIImage(named: placeHolderName)
            }
        }
    }
    
    var isFinish : Bool = false {
        didSet {
            self.photoImageView.image = UIImage(named: isFinish ? "public_photo_finish" : "publish_photo")
        }
    }
    
    var content : String? = "" {
        didSet {
            self.contentLb.text = content
        }
    }

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.isUserInteractionEnabled = true
        makeUI()
        makeConstraints()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        makeUI()
        makeConstraints()
    }
    
    private func makeUI() {
        self.addSubview(self.imageView)
        self.addSubview(self.photoView)
        self.photoView.addSubview(self.photoImageView)
        self.photoView.addSubview(self.contentLb)
        self.addSubview(self.btn)
    }
    
    private func makeConstraints() {
        self.imageView.snp.makeConstraints { make in
            make.top.left.bottom.equalTo(0)
            make.width.equalTo(129)
        }
        self.photoView.snp.makeConstraints { make in
            make.centerY.equalTo(self.imageView)
            make.right.equalTo(0)
        }
        self.photoImageView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.centerX.equalToSuperview()
            make.width.height.equalTo(45)
        }
        self.contentLb.snp.makeConstraints { make in
            make.top.equalTo(self.photoImageView.snp.bottom).offset(6)
            make.left.right.bottom.equalTo(0)
            make.height.equalTo(16)
        }
        self.btn.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    @objc private func btnClick() {
        onTapHandle?()
    }
}
